//
//  ViewController.h
//  外星球生活圈
//
//  Created by MR.ZHOU on 15/8/12.
//  Copyright (c) 2015年 MR.ZHOU. All rights reserved.
//

#import "DSHTTPClient.h"

//
//http://192.168.0.2/
#define kBaseURL @"http://www.qd-life.com/"

@implementation DSHTTPClient

+ (DSHTTPClient *)shareInatance{
    
   static DSHTTPClient *client = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        client = [[DSHTTPClient alloc]initWithBaseURL:[NSURL URLWithString:kBaseURL]];
        client.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];

        //92
        //
    });
    
    return client;
    
}

//代理

+ (void)DSGET:(NSString *)url
    withParam:(NSDictionary *)param
 withDelegate:(id)delegate{
    
    [[self shareInatance]DSGET:url withParam:param withDelegate:delegate];
    
}

- (void)DSGET:(NSString *)url
    withParam:(NSDictionary *)param
 withDelegate:(id)delegate{
    
    _delegate = delegate;
    
    [self GET:url parameters:param success:^(NSURLSessionDataTask *task, id responseObject) {
        NSString *status = responseObject[@"status"];
        
        if ( [status isEqualToString:@"success"]) {
            
            if ([_delegate respondsToSelector:@selector(didSuccessRequest:)]) {
                [_delegate didSuccessRequest:responseObject];
            }
            
        }else if ([status isEqualToString:@"error"]){
            if ([_delegate respondsToSelector:@selector(didErrorRequest:)]) {
                [_delegate didErrorRequest:@"请求失败，请稍后再试！"];
            }
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if ([_delegate respondsToSelector:@selector(didFailedRequest:)]) {
            [_delegate didFailedRequest:error];
        }
    }];
    
}

//块
+ (void)DSGET:(NSString *)url
    withParam:(NSDictionary *)param
withSuccessBlock:(successBlock)success
withFailedBlock:(failedBlock)failed
withErrorBlock:(errorBlock)error{
    
    [[self shareInatance]GET:url parameters:param success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSString *status = responseObject[@"status"];
        
        if ( [status isEqualToString:@"success"]) {
            
            success(responseObject);
            
        }else if ([status isEqualToString:@"error"]){
            error(@"请求失败，请稍后再试！");
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        failed(error);
        
    }];
    
}

+ (void)DSPOST:(NSString *)url
     withParam:(NSDictionary *)param
withSuccessBlock:(successBlock)success
withFailedBlock:(failedBlock)failed
withErrorBlock:(errorBlock)error{
    
    [[self shareInatance]POST:url parameters:param success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSString *status = responseObject[@"status"];
        
        if ( [status isEqualToString:@"success"]) {
            
            success(responseObject);
            
        }else if ([status isEqualToString:@"error"]){
            error(@"请求失败，请稍后再试！");
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        failed(error);
        
    }];
    
}
@end
